﻿@model OrderModel
@using Grand.Domain.Tax;
@using Grand.Domain.Catalog;
<vc:admin-widget widget-zone="order_details_products_top" additional-data="Model" />
<div class="table-responsive">
    <table class="table table-bordered table-striped table-hover">
        <tr>
            <td>
                @foreach (var item in Model.Items)
                {
                    <script>
                    $(document).ready(function() {
                        toggleOrderItemEdit@(item.Id)(false);
                        });
                    </script>

                    <script>
                    function toggleOrderItemEdit@(item.Id) (editMode) {
                        if (editMode) {
                            $('#pnlEditPvUnitPrice@(item.Id)').show();
                                $('#pnlEditPvQuantity@(item.Id)').show();
                                $('#pnlEditPvDiscount@(item.Id)').show();
                                $('#pnlEditPvPrice@(item.Id)').show();
                                $('#btnEditOrderItem@(item.Id)').hide();
                                $('#btnDeleteOrderItem@(item.Id)').hide();
                                $('#btnSaveOrderItem@(item.Id)').show();
                                $('#btnCancelOrderItem@(item.Id)').show();
                            }
                            else {
                                $('#pnlEditPvUnitPrice@(item.Id)').hide();
                                $('#pnlEditPvQuantity@(item.Id)').hide();
                                $('#pnlEditPvDiscount@(item.Id)').hide();
                                $('#pnlEditPvPrice@(item.Id)').hide();
                                $('#btnEditOrderItem@(item.Id)').show();
                                $('#btnDeleteOrderItem@(item.Id)').show();
                                $('#btnSaveOrderItem@(item.Id)').hide();
                                $('#btnCancelOrderItem@(item.Id)').hide();
                            }
                        }
                    </script>

                }
                <table class="table table-bordered table-striped table-hover">
                    <col />
                    <col />
                    @if (Model.HasDownloadableProducts)
                    {
                        <col />
                    }
                    <col />
                    <col />
                    <col />
                    <col />
                    @if (!Model.IsLoggedInAsVendor)
                    {
                        <col />
                    }
                    <thead>
                        <tr>
                            <th>
                                @T("Admin.Orders.Products.Picture")
                            </th>
                            <th>
                                @T("Admin.Orders.Products.ProductName")
                            </th>
                            @if (Model.HasDownloadableProducts)
                            {
                                <th>
                                    @T("Admin.Orders.Products.Download")
                                </th>
                            }
                            <th>
                                @T("Admin.Orders.Products.Price")
                            </th>
                            <th>
                                @T("Admin.Orders.Products.Quantity")
                            </th>
                            @if (Model.Items.Sum(x => x.DiscountInclTaxValue) > 0)
                            {
                                <th>
                                    @T("Admin.Orders.Products.Discount")
                                </th>
                            }
                            @if (Model.Items.Sum(x => x.CommissionValue) > 0)
                            {
                                <th>
                                    @T("Admin.Orders.Products.Commission")
                                </th>
                            }
                            <th>
                                @T("Admin.Orders.Products.Total")
                            </th>
                            @if (!Model.IsLoggedInAsVendor)
                            {
                                <th>
                                    @T("Admin.Common.Edit")
                                </th>
                            }
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var item in Model.Items)
                        {
                            <tr>
                                <td>
                                    <img src="@item.PictureThumbnailUrl" alt="" title="" />
                                </td>
                                <td>
                                    <div>
                                        <em><a href="@Url.Action("Edit", "Product", new { id = item.ProductId })">@item.ProductName</a></em>
                                        @if (!string.IsNullOrEmpty(item.AttributeInfo))
                                        {
                                            <p>
                                                @Html.Raw(item.AttributeInfo)
                                            </p>
                                        }
                                        @if (!string.IsNullOrEmpty(item.RecurringInfo))
                                        {
                                            <p>
                                                @Html.Raw(item.RecurringInfo)
                                            </p>
                                        }
                                        @if (!string.IsNullOrEmpty(item.RentalInfo))
                                        {
                                            <p>
                                                @Html.Raw(item.RentalInfo)
                                            </p>
                                        }
                                        @if (!string.IsNullOrEmpty(item.Sku))
                                        {
                                            <p>
                                                <strong>@T("Admin.Orders.Products.SKU")</strong><text>:</text>
                                                @item.Sku
                                            </p>
                                        }
                                        @if (!string.IsNullOrEmpty(item.VendorName))
                                        {
                                            <p>
                                                <strong>@T("Admin.Orders.Products.Vendor")</strong><text>:</text>
                                                @item.VendorName
                                            </p>
                                        }
                                        @if (item.ReturnRequestIds.Count > 0)
                                        {
                                            <p>
                                                @T("Admin.Orders.Products.ReturnRequests")<text>:</text>
                                                @for (int i = 0; i < item.ReturnRequestIds.Count; i++)
                                                {
                                                    <a href="@Url.Action("Edit", "ReturnRequest", new { id = item.ReturnRequestIds[i] })" class="k-button"><span class="k-icon k-i-eye"></span>&nbsp;@T("Admin.ReturnRequests.View")</a>
                                                    if (i != item.ReturnRequestIds.Count - 1)
                                                    {
                                                        <text>,</text>
                                                    }
                                                }
                                            </p>
                                        }
                                        @if (item.PurchasedGiftCardIds.Count > 0)
                                        {
                                            <p>
                                                @T("Admin.Orders.Products.GiftCards")<text>:</text>
                                                @for (int i = 0; i < item.PurchasedGiftCardIds.Count; i++)
                                                {
                                                    <a href="@Url.Action("Edit", "GiftCard", new { id = item.PurchasedGiftCardIds[i] })" class="k-button"><span class="k-icon k-i-eye"></span>&nbsp;@T("Admin.Orders.Products.GiftCard.View")</a>
                                                    if (i != item.PurchasedGiftCardIds.Count - 1)
                                                    {
                                                        <text>,</text>
                                                    }
                                                }
                                            </p>
                                        }
                                    </div>
                                </td>
                                @if (Model.HasDownloadableProducts)
                                {
                                    <td>
                                        @if (item.IsDownload)
                                        {
                                            <div>
                                                @string.Format(T("Admin.Orders.Products.Download.DownloadCount").Text, item.DownloadCount)
                                                <input type="submit" name="btnResetDownloadCount@(item.Id)" value="@T("Admin.Orders.Products.Download.ResetDownloadCount")" title="@T("Admin.Orders.Products.Download.ResetDownloadCount.Title")" id="btnResetDownloadCount@(item.Id)" class="k-button" />
                                                <hr />
                                            </div>
                                            if (item.DownloadActivationType == DownloadActivationType.Manually)
                                            {
                                                <div>
                                                    <input type="submit" name="btnPvActivateDownload@(item.Id)" @(item.IsDownloadActivated ? " value=" + T("Admin.Orders.Products.Download.Deactivate") : " value=" + T("Admin.Orders.Products.Download.Activate")) id="btnPvActivateDownload@(item.Id)" class="k-button" />
                                                    <hr />
                                                </div>
                                            }
                                            <div>
                                                <strong>
                                                    @T("Admin.Orders.Products.License")
                                                </strong>
                                                <br />
                                                @if (item.LicenseDownloadGuid != Guid.Empty)
                                                {
                                                    <a href="@Url.Action("DownloadFile", "Download", new { downloadGuid = item.LicenseDownloadGuid })" class="btn purple">@T("Admin.Orders.Products.License.DownloadLicense")</a>
                                                }
                                                <br />
                                                <input type="submit" id="pvlicensefile" name="pvlicensefile" value="@T("Admin.Orders.Products.License.UploadFile")" onclick="javascript:OpenWindow('@(Url.Action("UploadLicenseFilePopup", "Order", new { id = Model.Id, orderItemId = item.Id, btnId = "btnRefreshPage", formId = "order-form" }))', 550, 300, true); return false;" class="k-button" />
                                            </div>
                                        }
                                    </td>
                                }
                                <td>
                                    @if (Model.AllowCustomersToSelectTaxDisplayType)
                                    {
                                        @item.UnitPriceInclTax
                                        <br />
                                        @item.UnitPriceExclTax
                                    }
                                    else
                                    {
                                        switch (Model.TaxDisplayType)
                                        {
                                            case TaxDisplayType.ExcludingTax:
                                                {
                                                    @item.UnitPriceExclTax
                                                }
                                                break;
                                            case TaxDisplayType.IncludingTax:
                                                {
                                                    @item.UnitPriceInclTax
                                                }
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                    <div id="pnlEditPvUnitPrice@(item.Id)">
                                        <table class="order-edit">
                                            <tr>
                                                <td>
                                                    @T("Admin.Orders.Products.Edit.InclTax")
                                                </td>
                                                <td>
                                                    <input name="pvUnitPriceInclTax@(item.Id)" type="text" value="@item.UnitPriceInclTaxValue" id="pvUnitPriceInclTax@(item.Id)" class="adminInput" style="width:100px;" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    @T("Admin.Orders.Products.Edit.ExclTax")
                                                </td>
                                                <td>
                                                    <input name="pvUnitPriceExclTax@(item.Id)" type="text" value="@item.UnitPriceExclTaxValue" id="pvUnitPriceExclTax@(item.Id)" class="adminInput" style="width:100px;" />
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                </td>
                                <td>
                                    @item.Quantity
                                    <div id="pnlEditPvQuantity@(item.Id)">
                                        <table class="order-edit">
                                            <tr>
                                                <td colspan="2">
                                                    <input name="pvQuantity@(item.Id)" type="text" value="@item.Quantity" id="pvQuantity@(item.Id)" class="adminInput" style="width:100px;" />
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                </td>
                                @if (Model.Items.Sum(x => x.DiscountInclTaxValue) > 0)
                                {
                                    <td>
                                        @if (Model.AllowCustomersToSelectTaxDisplayType)
                                        {
                                            @item.DiscountInclTax
                                            <br />
                                            @item.DiscountExclTax
                                        }
                                        else
                                        {
                                            switch (Model.TaxDisplayType)
                                            {
                                                case TaxDisplayType.ExcludingTax:
                                                    {
                                                        @item.DiscountExclTax
                                                    }
                                                    break;
                                                case TaxDisplayType.IncludingTax:
                                                    {
                                                        @item.DiscountInclTax
                                                    }
                                                    break;
                                                default:
                                                    break;
                                            }
                                        }

                                        <div id="pnlEditPvDiscount@(item.Id)">
                                            <table class="order-edit">
                                                <tr>
                                                    <td>
                                                        @T("Admin.Orders.Products.Edit.InclTax")
                                                    </td>
                                                    <td>
                                                        <input name="pvDiscountInclTax@(item.Id)" type="text" value="@item.DiscountInclTaxValue" id="pvDiscountInclTax@(item.Id)" class="adminInput" style="width:100px;" />
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                        @T("Admin.Orders.Products.Edit.ExclTax")
                                                    </td>
                                                    <td>
                                                        <input name="pvDiscountExclTax@(item.Id)" type="text" value="@item.DiscountExclTaxValue" id="pvDiscountExclTax@(item.Id)" class="adminInput" style="width:100px;" />
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </td>
                                }
                                @if (Model.Items.Sum(x => x.CommissionValue) > 0)
                                {
                                    <td>
                                        <div>@item.Commission</div>
                                    </td>
                                }
                                <td>
                                    @if (Model.AllowCustomersToSelectTaxDisplayType)
                                    {
                                        @item.SubTotalInclTax
                                        <br />
                                        @item.SubTotalExclTax
                                    }
                                    else
                                    {
                                        switch (Model.TaxDisplayType)
                                        {
                                            case TaxDisplayType.ExcludingTax:
                                                {
                                                    @item.SubTotalExclTax
                                                }
                                                break;
                                            case TaxDisplayType.IncludingTax:
                                                {
                                                    @item.SubTotalInclTax
                                                }
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                    <div id="pnlEditPvPrice@(item.Id)">
                                        <table class="order-edit">
                                            <tr>
                                                <td>
                                                    @T("Admin.Orders.Products.Edit.InclTax")
                                                </td>
                                                <td>
                                                    <input name="pvPriceInclTax@(item.Id)" type="text" value="@item.SubTotalInclTaxValue" id="pvPriceInclTax@(item.Id)" class="adminInput" style="width:100px;" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    @T("Admin.Orders.Products.Edit.ExclTax")
                                                </td>
                                                <td>
                                                    <input name="pvPriceExclTax@(item.Id)" type="text" value="@item.SubTotalExclTaxValue" id="pvPriceExclTax@(item.Id)" class="adminInput" style="width:100px;" />
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                </td>

                                @if (!Model.IsLoggedInAsVendor)
                                {
                                    <td align="center" style="width: 15%;">
                                        <button type="submit" name="btnEditOrderItem@(item.Id)" id="btnEditOrderItem@(item.Id)" onclick="toggleOrderItemEdit@(item.Id)(true);return false; " class="k-button"><span class="k-icon k-i-edit"></span>&nbsp;@T("Admin.Common.Edit")</button>
                                        <button type="submit" name="btnDeleteOrderItem@(item.Id)" id="btnDeleteOrderItem@(item.Id)" onclick="return confirm('@T("Admin.Common.AreYouSure")'); " class="k-button"><span class="k-icon k-i-delete"></span>&nbsp;@T("Admin.Common.Delete")</button>
                                        <button type="submit" name="btnSaveOrderItem@(item.Id)" id="btnSaveOrderItem@(item.Id)" style="display:none;" onclick="return confirm('@T("Admin.Common.AreYouSure")'); " class="k-button"><span class="k-icon k-i-save"></span>&nbsp;@T("Admin.Common.Save")</button>
                                        <button type="submit" name="btnCancelOrderItem@(item.Id)" id="btnCancelOrderItem@(item.Id)" onclick="toggleOrderItemEdit@(item.Id)    (false);return false; " class="k-button"><span class="k-icon k-i-cancel"></span>&nbsp;@T("Admin.Common.Cancel")</button>
                                    </td>
                                }
                            </tr>
                        }
                    </tbody>
                </table>
            </td>
        </tr>
    </table>
</div>
    <vc:admin-widget widget-zone="order_details_products_bottom" additional-data="Model" />

    @if (!String.IsNullOrEmpty(Model.CheckoutAttributeInfo) && !Model.IsLoggedInAsVendor)
    {
        <div class="note note-info">
            @Html.Raw(Model.CheckoutAttributeInfo)
        </div>
    }
    @if (!Model.IsLoggedInAsVendor)
    {
        <div class="form-actions">
            <button type="submit" id="btnAddNewProduct" name="btnAddNewProduct" onclick=" javascript:setLocation('@(Url.Action("AddProductToOrder", "Order", new { orderId = Model.Id }))'); return false; " class="k-button"><i class="fa fa-plus"></i>&nbsp;@T("Admin.Orders.Products.AddNew")</button>
        </div>
    }


